<?php

namespace App\Http\Controllers;

use App\Models\Wechat;
use Illuminate\Http\Request;

class ManageController extends Controller{

    /**
     * 账户列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request){
        $params = [
            'name'   => (string)$request->get('name',''),
        ];
        $lists = Wechat::select('id','account','avatar','name','type','is_auth','scan_at')
            ->where(function ($query) use ($params) {
                if($params['name']) {
                    $query ->where('name','like','%' . $params['name'] . '%')
                        ->orWhere('account','like','%' . $params['name'] . '%')
                        ->orWhere('id',$params['name']);
                }
            })
            ->orderBy('scan_at', 'desc')
            ->paginate(10);
        return $this->returnJson(0,'',$lists);
    }

    /**
     * 账户删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destory(Request $request){
        try {
            $id = $request->get('id');
            Wechat::destroy($id);
            return $this->returnJson(0);
        } catch (\Exception $e) {
            return $this->returnJson(1, $e->getMessage());
        }

    }


}